# rm(list = ls());
gc()

# library(ellipse)
# library(ggplot2)
# library(tidyverse)

dir.create("plots/example/", recursive = TRUE)

a <-list()
a[[1]] <- c(1,0,0,1)
a[[2]] <- c(1,-0.5,-0.5,1)
a[[3]] <- c(1,0.5,0.5,1)


radius <- c(1.72,2.12,3.5)
origin <- data.frame(x=0, y=0)
plots <- list()
l <- 3 # lim
quad <- data.frame(x=c(0,0,-l,-l, 0,0,l,l), y=c(0,-l,-l,0, 0,l,l,0), t=c(rep(1,4), rep(2,4)))

# Proposal
prop_df <- data.frame("prop"=as.factor(c("Yea[j]","Nay[j]")),x=c(1,1),y=c(1,-1))


for (i in 1:length(a)){
  e <- as.data.frame(ellipse(matrix(a[[i]], 2,2)))
  
  plots[[i]] <- ggplot(e) + xlab("Dimension 1") + ylab("Dimension 2") + xlim(-l,l) + ylim(-l,l) +
    geom_path(aes(x=x/radius[3], y=y/radius[3])) +
    geom_path(aes(x=x/radius[1], y=y/radius[1])) +
    geom_path(aes(x=x/radius[2], y=y/radius[2])) +
    #geom_point(x=0,y=0) + geom_text(label="v", x=0, y=0, vjust=1.5) +
    geom_point(data=origin,aes(x,y)) + 
    geom_point(data=prop_df,aes(x,y)) +
    annotate("text", label="theta[i]", x=0, y=-.3, size=7, parse=TRUE) +
    geom_text(data=prop_df,aes(x=x,y=y,label=c("Y[j]","N[j]")), nudge_y=-.3, size=7, parse=TRUE) +
    #annotate(label="v", x=0, y=0, vjust=1.5) + 
    coord_fixed() + theme_minimal() +     
    geom_polygon(data=quad, aes(x=x, y=y, group=t), col="0", size=7, alpha=0.05) +
    theme(plot.title = element_text(hjust = 0.5), text = element_text(size=20))
}
plots[[3]]


plots[[1]] <-  plots[[1]] + theme()#+ ggtitle("Separable")
plots[[2]] <-  plots[[2]] #+ ggtitle("Substitute")
plots[[3]] <-  plots[[3]] #+ ggtitle("Complement")
# plots[[4]] <-  plots[[4]] + ggtitle(expression(paste("A = ", bgroup("[", atop("2  -1", "-1  1"),"]"))))


pdf("plots/example/indifference_sep.pdf",width = 6, height = 6)
plots[[1]] 
dev.off()

pdf("plots/example/indifference_sub.pdf",width = 6, height = 6)
plots[[2]] 
dev.off()

pdf("plots/example/indifference_com.pdf",width = 6, height = 6)
plots[[3]] 
dev.off()